<?php
/**
 * Created by PhpStorm.
 * User: 胥九庆
 * Date: 2023/01/09
 * Time: 16:23
 */
declare (strict_types=1);

namespace app\time\controller;

use AliPay\App;
use app\common\model\Logs;
use app\common\model\OrderRefund;
use app\common\model\OrderTemporary;
use app\common\model\OrderVideo;
use app\common\model\User;
use WeChat\Pay;

class Callback
{
    public function alipay()
    {
        try {
            $route = 'logs/alipay.log';
            Logs::write($route, '支付回调：'.request()->action());
            $pay = App::instance(config('pay')['alipay']);
            $data = $pay->notify();
            Logs::write($route, '支付回调结果：'.json_encode($data));
            if (!in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
                throw new \Exception('回调验证失败:' .json_encode($data));
            }
            //更新订单状态，支付完成
            Logs::write($route, '支付成功，订单号:'.$data['out_trade_no']);

        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());

            return 'FAIL';
        }

        return 'SUCCESS';
    }

    public function wechat()
    {
        try {
            $route = 'logs/wechat.log';
            Logs::write($route, '支付回调：'.request()->action());
            $wechat = Pay::instance(config('pay')['wechat']);
            $data = $wechat->getNotify();
            Logs::write($route, '支付回调结果：'.json_encode($data));
            if ($data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
                throw new \Exception('回调验证失败:' .json_encode($data));
            }
            //更新订单状态，支付完成
            Logs::write($route, '支付成功，订单号:'.$data['out_trade_no']);
            $result = $wechat->getNotifySuccessReply();

        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());
            return 'FAIL';
        }

        return $result;
    }

    #############################################################################################################################

    /**
     * 商城-支付宝回调
     */
    public function mallAlipay()
    {
        try {
            $route = 'logs/mallAlipay.log';
            Logs::write($route, '支付回调：'.request()->action());
            $pay = App::instance(config('pay')['alipay']);
            $data = $pay->notify();
            Logs::write($route, '支付回调结果：'.json_encode($data));
            if (!in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
                throw new \Exception('回调验证失败:' .json_encode($data));
            }
            //更新订单状态，支付完成
            $info = OrderTemporary::where(['sn' => $data['out_trade_no']])->where('is_del', 0)->find();
            if ($info && $info->getData('status') == 1) {
                $result = OrderTemporary::operations($info, User::find($info->uid), 'alipay');
                if ($result['code'] != 1) {
                    throw new \Exception('支付失败，订单号:'.$info->sn.'->'.$result['msg']);
                }
            } else {
                throw new \Exception('未查询到订单或订单已支付，订单号:'.$data['out_trade_no']);
            }
            Logs::write($route, '支付成功，订单号:'.$data['out_trade_no']);

        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());

            return 'FAIL';
        }

        return 'SUCCESS';
    }

    /**
     * 商城-微信回调
     */
    public function mallWechat()
    {
        try {
            $route = 'logs/mallWechat.log';
            Logs::write($route, '支付回调：'.request()->action());
            $wechat = Pay::instance(config('pay')['mall']['JSAPI']);
            $data = $wechat->getNotify();
            Logs::write($route, '支付回调结果：'.json_encode($data));
            if ($data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
                throw new \Exception('回调验证失败:' .json_encode($data));
            }
            //更新订单状态，支付完成
            $info = OrderTemporary::where(['sn' => $data['out_trade_no']])->where('is_del', 0)->find();
            if ($info && $info->getData('status') == 1) {
                $result = OrderTemporary::operations($info, User::find($info->uid), 'wechat');
                if ($result['code'] != 1) {
                    throw new \Exception('支付失败，订单号:'.$info->sn.'->'.$result['msg']);
                }
            } else {
                throw new \Exception('未查询到订单或订单已支付，订单号:'.$data['out_trade_no']);
            }
            Logs::write($route, '支付成功，订单号:'.$data['out_trade_no']);
            $result = $wechat->getNotifySuccessReply();

        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());

            return 'FAIL';
        }

        return $result;
    }

    /**
     * 商城-微信回调
     */
    public function mallWechatHandle()
    {
        try {
            $route = 'logs/mallWechat.log';
            Logs::write($route, '支付回调：'.request()->action());
            $wechat = Pay::instance(config('pay')['mall']['JSAPI']);
            $data = request()->param();
            Logs::write($route, '支付回调结果：'.json_encode($data));
            if ($data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
                throw new \Exception('回调验证失败:' .json_encode($data));
            }
            //更新订单状态，支付完成
            $info = OrderTemporary::where(['sn' => $data['out_trade_no']])->where('is_del', 0)->find();
            if ($info && $info->getData('status') == 1) {
                $result = OrderTemporary::operations($info, User::find($info->uid), 'wechat');
                if ($result['code'] != 1) {
                    throw new \Exception('支付失败，订单号:'.$info->sn.'->'.$result['msg']);
                }
            } else {
                throw new \Exception('未查询到订单或订单已支付，订单号:'.$data['out_trade_no']);
            }
            Logs::write($route, '支付成功，订单号:'.$data['out_trade_no']);
            $result = $wechat->getNotifySuccessReply();

        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());

            return 'FAIL';
        }

        return $result;
    }


}